package com.futvan.z.framework.util;

import com.futvan.z.framework.common.bean.Code;
import com.futvan.z.framework.common.bean.Result;
import org.apache.catalina.startup.Bootstrap;

import java.io.File;

/**
 * Tomcat工具类
 * @author 4223947@qq.com
 *
 */
public class TomcatUtil {

	/**
	 * 启动Tomcat
	 * @param tomcat_path
	 * @return
	 */
	public static Result startup(File tomcat_path) {
		Result result = new Result();
		try {
			Bootstrap bs = new Bootstrap();
			bs.start();
		} catch (Exception e) {
			result.setCode(Code.ERROR);
			result.setMsg("Tomcat Startup Error|"+e.getMessage());
		}
		return result;
	}
	
	/**
	 * 关闭Tomcat
	 * @param tomcat_path
	 * @return
	 */
	public static Result shutdown(File tomcat_path) {
		Result result = new Result();
		try {
			Bootstrap bs = new Bootstrap();
			bs.stop();
		} catch (Exception e) {
			result.setCode(Code.ERROR);
			result.setMsg("Tomcat shutdown Error|"+e.getMessage());
		}
		return result;
	}

    /**
     * 获取项目在Tomcat运行时根目录
	 * @return String
	 */
	public static String getTomcatProjectPath(){
		//获取当前类文件运行时路径
		String path = String.valueOf(Thread.currentThread().getContextClassLoader().getResource(""));
		//去掉头信息，WEB-INF/classes信息，返回项目运行时根目录
		if(path.indexOf("WEB-INF/classes")>0){
			path = path.substring(6,path.indexOf("WEB-INF/classes"));
		}else{
			path = path.substring(6,path.length()-1);
		}
		return path;
	}
	
}
